import request from '@/utils/request'
import { removeToken } from '@/utils/storage';


/**
 * 保存用户信息
 * @param {*} payload 
 * @returns 
 */
export const saveMineInfo = (payload) => {
    return {
        type: 'mine/saveInfo',
        payload
    }
};




/**
 * 获取用户信息
 * @returns 
 */
export const getMineInfo = () => {
    return async dispatch => {
        const res = await request.get('/user')
        dispatch(saveMineInfo(res.data))
    }
};

/**
 * 保存用户信息
 * @param {*} payload 
 * @returns 
 */
export const saveProfile = (payload) => {
    return {
        type: 'mine/profile',
        payload
    }
};


/**
 * 获取用户详细信息
 * @returns 
 */
export const getProfile = () => {
    return async dispatch => {
        const res = await request.get('/user/profile')
        dispatch(saveProfile(res.data))
    }
};

/**
 * 获取用户详细信息
 * @returns 
 */
export const updateUserInfo = (type, value) => {
    return async dispatch => {
        await request.patch('/user/profile', {
            [type]: value
        })
        dispatch(getProfile())
    }
};

/**
 * 获取用户详细信息
 * @returns 
 */
export const updatePhoto = (fd) => {
    return async dispatch => {
        await request.patch('/user/photo', fd)
        dispatch(getProfile())
    }
};

/**
 * 退出登陆
 * @returns 
 */
export const logout = () => {
    return async dispatch => {
        await removeToken()
        dispatch({
            type: 'logout'
        })
    }
};
